package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sofa.common.profile.StringUtil;
import com.awspaas.user.apps.processforsap.util.HttpClientGwUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class GWTaskValidateTS extends InterruptListener implements InterruptListenerInterface {
    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {
        /**
         * 电子
         */
        boolean isty = ctx.isChoiceActionMenu("同意");

        if (isty) {
            //校验数据
            BO bo = ctx.getBO("BO_EU_MM_GW");
            //获取物料组
            String matlGroup = bo.getString("MATL_GROUP");
            String matlType = bo.getString("MATL_TYPE");

            String[] type = {"FH00", "FS00"};


            List<String> bonMars = new ArrayList<>();
            if ("Z001".equals(matlType)){
                bonMars.add("0");
            }
            if ("Z005".equals(matlType)){
                bonMars.add("1");
            }
            if ("Z002".equals(matlType)&&("1-WF-00/1-IC-00/1-SB-00").contains(matlGroup)){
                bonMars.add("1");
            }
            for (String a : type) {
                for (String bondMar : bonMars) {
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String tokenUrl = "http://10.21.89.45:8099/pms/api/session";
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("userno", a.equals("FH00") ? "3312964743-ERP" : "33129661F0-ERP");
                    jsonObject.put("password", a.equals("FH00") ? "zhgw@123" : "zhgw@123");
                    jsonObject.put("isVerify", true);
                    String result = HttpClientGwUtil.doPost(tokenUrl, jsonObject.toJSONString());
                    JSONObject jsonResult = JSONObject.parseObject(result);
                    System.out.println("--------------------------aaa+" + result);

                    if (jsonResult.getBoolean("success")) {
                        JSONObject data = jsonResult.getJSONObject("data");
                        String token = data.getString("accessToken");

                        //开始调用业务代码
                        String url = "http://10.21.89.45:8099/gw-edi-up/api/open/v1/erpMat/insertErpMat";
                        JSONObject allData = new JSONObject();
                        JSONArray dataInfo = new JSONArray();
                        JSONObject jsonObject1 = new JSONObject();

                        //获取商品编码
                        String spbm = bo.getString("SPBM");
                        if (StringUtil.isNotEmpty(spbm)) {
                            if (spbm.contains("8542319099") || spbm.contains("8542339000") || spbm.contains("8542399000")) {
                                jsonObject1.put("gModel", bo.getString("PPLX") + "|3|" + bo.getString("YYCJ") + "|" + bo.getString("GN") + "|" + bo.getString("SFFZ") + "|" + bo.getString("SFQG") + "|" + bo.getString("PP") + "|" + bo.getString("XH") + "|" + bo.getString("SFLC"));
                            } else if (spbm.contains("8542329010") || spbm.contains("8542329090")) {
                                jsonObject1.put("gModel", bo.getString("PPLX") + "|3|" + bo.getString("YYCJ") + "|" + bo.getString("GN") + "|" + bo.getString("SFFZ") + "|" + bo.getString("SFQG") + "|" + bo.getString("PP") + "|" + bo.getString("XH") + "|" + bo.getString("SFLC") + "|" + bo.getString("RL"));
                            } else if (spbm.contains("8532290000")) {
                                jsonObject1.put("gModel", bo.getString("PPLX") + "|3|" + bo.getString("JGLX") + "|" + bo.getString("JZLX") + "|" + bo.getString("PP") + "|" + bo.getString("XH"));
                            } else if (spbm.contains("8534009000") || spbm.contains("8534001000")) {
                                jsonObject1.put("gModel", bo.getString("PPLX") + "|3|"+ bo.getString("DLCS") + "|" + bo.getString("SFBDYJXYJ") + "|" + bo.getString("SFBDYYYDQYJ") + "|" + bo.getString("PP") + "|" + bo.getString("XH"));
                            }
                        }
                        jsonObject1.put("bondMark", bondMar);

//                            jsonObject1.put("gMark", "1-IC-00".equals(matlGroup) ? "E" : "I");
                        jsonObject1.put("gMark", "I");

                        jsonObject1.put("facGNo", bo.getString("MATERIAL"));
                        jsonObject1.put("copGNo", a.equals("FH00") ? bo.getString("LJ_YXDZ") : bo.getString("LJ_BDT"));
                        jsonObject1.put("copGName", bo.getString("SPMC"));
//                    jsonObject1.put("copGNameEn",);
                        jsonObject1.put("copGModelEn", bo.getString("XH"));
                        jsonObject1.put("codets", bo.getString("SPBM"));
//                            jsonObject1.put("createDepartment", "客服处");
                        jsonObject1.put("unitErp", bo.getString("SBJLDW"));
                        jsonObject1.put("supplierCode", bo.getString("KH"));
//                            jsonObject1.put("factory", bo.getString("PLANT"));
                        jsonObject1.put("lastUpdateTime", simpleDateFormat.format(date));
                        jsonObject1.put("createDepartment", SDK.getORGAPI().getDepartmentById(SDK.getORGAPI().getUser(ctx.getProcessInstance().getCreateUser()).getDepartmentId()).getName());
                        jsonObject1.put("makeInsert", ctx.getUserContext().getUserName() + ctx.getUserContext().getUID());
                        jsonObject1.put("gNo", "");
                        dataInfo.add(jsonObject1);
                        allData.put("data", dataInfo);
                        String result1 = HttpClientGwUtil.doPost1(url, allData.toJSONString(), token);
                    } else {
                        throw new BPMNError("0331", "意外的接口调用错误！");
                    }
                }
            }

        }
        return true;
    }
}
